﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing.Test            Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: TaskProcessingWorkerTest.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System.Threading;
using Hyldahl.Services.TaskProcessing.Test.Dummy;
using Hyldahl.Services.TaskProcessing.Workers;
using NUnit.Framework;

namespace Hyldahl.Services.TaskProcessing.Test
{
    
    
    /// <summary>
    ///This is a test class for TaskProcessingWorkerTest and is intended
    ///to contain all TaskProcessingWorkerTest Unit Tests
    ///</summary>
    [TestFixture()]
    public class TaskProcessingWorkerTest
    {
        /// <summary>
        ///A test for TaskProcessingWorker Constructor
        ///</summary>
        [Test()]
        public void TaskProcessingWorkerConstructorTest()
        {
            TaskProcessingWorker target = new TaskProcessingWorker(new TaskProcessingController(new DummyContext()), "TPW");
        }

        /// <summary>
        ///A test for TaskProcessingWorker
        ///</summary>
        [Test()]
        public void TaskProcessingWorkerUnknownTaskTypeTest()
        {
            TaskProcessingWorker target = new TaskProcessingWorker(
                new TaskProcessingController(
                    new DummyContext(
                        new ErrorDummyTaskDataProvider(
                            TaskDataProviderError.UnknownTaskType
                        )
                    )
                ), "TPW");

            target.Start();

            // No errors expected
            Thread.Sleep(500);

            target.Stop();
            target.WaitForCompletion();
        }

        /// <summary>
        ///A test for TaskProcessingWorker
        ///</summary>
        [Test()]
        public void TaskProcessingWorkerGetUnProcessedTaskErrorTest()
        {
            TaskProcessingWorker target = new TaskProcessingWorker(
                new TaskProcessingController(
                    new DummyContext(
                        new ErrorDummyTaskDataProvider(
                            TaskDataProviderError.GetUnProcessedTaskError
                        )
                    )
                ), "TPW");

            target.Start();

            // No errors expected
            Thread.Sleep(500);

            target.Stop();
            target.WaitForCompletion();
        }

        /// <summary>
        ///A test for TaskProcessingWorker
        ///</summary>
        [Test()]
        public void TaskProcessingWorkerInvalidFibonaciTaskDataTest()
        {
            TaskProcessingWorker target = new TaskProcessingWorker(
                new TaskProcessingController(
                    new DummyContext(
                        new ErrorDummyTaskDataProvider(
                            TaskDataProviderError.InvalidFibonaciTaskData
                        )
                    )
                ), "TPW");

            target.Start();

            // No errors expected
            Thread.Sleep(500);

            target.Stop();
            target.WaitForCompletion();
        }

        /// <summary>
        ///A test for TaskProcessingWorker
        ///</summary>
        [Test()]
        public void TaskProcessingWorkerSaveTaskResultErrorTest()
        {
            TaskProcessingWorker target = new TaskProcessingWorker(
                new TaskProcessingController(
                    new DummyContext(
                        new ErrorDummyTaskDataProvider(
                            TaskDataProviderError.SaveTaskResultError
                        )
                    )
                ), "TPW");

            target.Start();

            // No errors expected
            Thread.Sleep(500);

            target.Stop();
            target.WaitForCompletion();

            Assert.IsTrue(((ErrorDummyTaskDataProvider)target.Controller.Context.DataStore).SaveTaskResultCalled);
        }

        /// <summary>
        ///A test for TaskProcessingWorker
        ///</summary>
        [Test()]
        public void TaskProcessingWorkerNoErrorsTest()
        {
            TaskProcessingWorker target = new TaskProcessingWorker(
                new TaskProcessingController(
                    new DummyContext(
                        new ErrorDummyTaskDataProvider(
                            TaskDataProviderError.None
                        )
                    )
                ), "TPW");

            target.Start();

            // No errors expected
            Thread.Sleep(500);

            target.Stop();
            target.WaitForCompletion();

            Assert.IsTrue(((ErrorDummyTaskDataProvider)target.Controller.Context.DataStore).SaveTaskResultCalled);
        }
    }
}
